package com.love.marriage.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 小程序微信支付配置
 */
@Configuration
public class WxMiniAppPayConfig {

    @Autowired
    private WeChatConfig weChatConfig;

    @Value("${wechat.pay.sandbox.enabled:false}")
    private boolean sandboxEnabled;

    /**
     * 小程序微信支付服务
     */
    @Bean("miniAppPayService")
    public WxPayService miniAppPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(weChatConfig.getAppId());
        payConfig.setMchId(weChatConfig.getMchid());
        payConfig.setMchKey(weChatConfig.getApiV2());
        payConfig.setApiV3Key(weChatConfig.getApiv3());
        payConfig.setKeyPath(weChatConfig.getKeyPath());
        payConfig.setPrivateKeyPath(weChatConfig.getPrivateKeyPath());
        payConfig.setPrivateCertPath(weChatConfig.getPrivateCertPath());
        payConfig.setNotifyUrl(weChatConfig.getNotifyUrl());
        payConfig.setUseSandboxEnv(sandboxEnabled); // 通过配置文件控制沙箱环境
        
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
} 